package com.zenithmind.user.pojo.domain;

import com.zenithmind.common.base.BaseEntity;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import io.swagger.v3.oas.annotations.media.Schema;

import java.util.Objects;

/**
 * 角色聚合根
 * 遵循单一职责原则：只负责角色信息管理
 * 遵循封装原则：通过方法控制状态变更
 */
@EqualsAndHashCode(callSuper = true)
@Getter
@NoArgsConstructor
@Schema(name = "角色实体", description = "角色聚合根")
public class Role extends BaseEntity {

    @Schema(description = "角色名称")
    private String roleName;

    @Schema(description = "角色编码")
    private String roleCode;

    @Schema(description = "角色描述")
    private String description;

    @Schema(description = "状态 (0-禁用, 1-启用)")
    private Integer status;

    /**
     * 构造函数 - 创建新角色
     */
    public Role(String roleName, String roleCode, String description) {
        this.roleName = Objects.requireNonNull(roleName, "角色名称不能为空");
        this.roleCode = Objects.requireNonNull(roleCode, "角色编码不能为空");
        this.description = description;
        this.status = 1; // 默认启用

        validateRoleName(roleName);
        validateRoleCode(roleCode);
    }

    /**
     * 更新角色信息
     */
    public void updateInfo(String roleName, String description) {
        if (roleName != null) {
            validateRoleName(roleName);
            this.roleName = roleName;
        }
        this.description = description;
    }

    /**
     * 启用角色
     */
    public void enable() {
        this.status = 1;
    }

    /**
     * 禁用角色
     */
    public void disable() {
        this.status = 0;
    }

    /**
     * 检查角色是否启用
     */
    public boolean isEnabled() {
        return this.status != null && this.status == 1;
    }

    /**
     * 检查是否为系统内置角色
     */
    public boolean isSystemRole() {
        return "ADMIN".equals(this.roleCode) ||
               "USER".equals(this.roleCode) ||
               "SUPER_ADMIN".equals(this.roleCode);
    }

    /**
     * 验证角色名称
     */
    private void validateRoleName(String roleName) {
        if (roleName == null || roleName.trim().isEmpty()) {
            throw new IllegalArgumentException("角色名称不能为空");
        }
        if (roleName.length() > 50) {
            throw new IllegalArgumentException("角色名称长度不能超过50个字符");
        }
    }

    /**
     * 验证角色编码
     */
    private void validateRoleCode(String roleCode) {
        if (roleCode == null || roleCode.trim().isEmpty()) {
            throw new IllegalArgumentException("角色编码不能为空");
        }
        if (roleCode.length() > 50) {
            throw new IllegalArgumentException("角色编码长度不能超过50个字符");
        }
        if (!roleCode.matches("^[A-Z_]+$")) {
            throw new IllegalArgumentException("角色编码只能包含大写字母和下划线");
        }
    }

    // MyBatis-Plus需要的setter方法（仅用于数据库映射）
    public void setRoleName(String roleName) { this.roleName = roleName; }
    public void setRoleCode(String roleCode) { this.roleCode = roleCode; }
    public void setDescription(String description) { this.description = description; }
    public void setStatus(Integer status) { this.status = status; }
}
